import Vue from 'vue';
import API from '../../../api/api';

const state = {
    role: null
};

const actions = {
    async getRole({commit}, uval) {
        var url = API.ROLE_URL + '?page=' + uval.pageNum + '&size=' + uval.pageSize;
        if (uval.roleName) {
            url += '&roleName=' + uval.roleName;
        }
        if (uval.sort) {
            url += '&sort=' + uval.sort;
        }
        var res = await Vue.http.get(url);
        return res;
    },
    async getRoleId({commit}, uval) {
        var url = API.ROLE_URL + uval.roleId;
        var res = await Vue.http.get(url);
        return res;
    },
    async addRole({commit}, uval) {
        var res = await Vue.http.post(API.POST_ROLE_REGISTER, uval);
        return res;
    },
    async updateRole({commit}, uval) {
        var url = API.ROLE_URL + uval.roleId;
        var res = await Vue.http.put(url, {
            roleName: uval.roleName,
            roleDesc: uval.roleDesc
        });
        return res;
    },
    async deleteRole({commit}, uval) {
        var url = API.ROLE_URL + uval.roleId;
        var res = await Vue.http.delete(url);
        return res;
    },
    async getRoleList({commit}) {
        var url = API.ROLE_LIST_URL;
        var res = await Vue.http.get(url);
        return res;
    },
    async getPermission({commit}) {
        var url = API.PERMISSION_URL;
        var res = await Vue.http.get(url);
        return res;
    },
    async assignPermission({commit}, uval) {
        var url = API.PERMISSION_URL + uval.roleId;
        var res = await Vue.http.post(url, {
            id: uval.data
        });
        return res;
    }
};

const mutations = {};

export default {
    state,
    actions,
    mutations
};
